﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CWT.ArticleModule
{
    public class CwtService : ICwtService
    {
        private ICwtFactory _factory;

        public CwtService(ICwtFactory factory)
        {
            _factory = factory;
        }

        public Company GetCompany()
        {
            using (CwtContext context = _factory.Get())
            {
                return context.Set<Company>().FirstOrDefault();
            }
        }

        public bool SaveCompany(Company company)
        {
            using (CwtContext context = _factory.Get())
            {
                if (context.Set<Company>().Where(p => p.Id == company.Id)
                    .Select(p => p.Id).FirstOrDefault() != default(Guid))
                {
                    // update
                    context.Entry(company).State = System.Data.EntityState.Modified;
                }
                else if (context.Set<Company>().FirstOrDefault() == null)
                {
                    // insert
                    context.Set<Company>().Add(company);
                }
                else
                {
                    // error
                    return false;
                }

                if (context.SaveChanges() > 0)
                {
                    return true;
                }
                return false;
            }
        }

        public bool SaveSummary(Summary summary)
        {
            using (CwtContext context = _factory.Get())
            {
                if (context.Set<Summary>()
                     .Where(p => p.Id == summary.Id)
                     .Select(p => p.Id).FirstOrDefault() == default(Guid))
                {
                    context.Set<Summary>().Add(summary);
                }
                else
                {
                    context.Entry(summary).State = System.Data.EntityState.Modified;
                }
                context.SaveChanges();
                return true;
            }
        }

        public bool DeleteSummary(Summary summary)
        {
            using (CwtContext context = _factory.Get())
            {
                if (context.Set<Summary>()
                     .Where(p => p.Id == summary.Id)
                     .Select(p => p.Id).FirstOrDefault() != default(Guid))
                {
                    context.Entry(summary).State = System.Data.EntityState.Deleted;
                }
                else
                {
                    return false;
                }
                context.SaveChanges();
                return true;
            }
        }

        public List<Summary> FindSummaries()
        {
            using (CwtContext context = _factory.Get())
            {
                return context.Set<Summary>()
                    .OrderBy(p => p.Order)
                    .ToList();
            }
        }
    }
}
